package com.keeley.spider.ext;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.keeley.spider.model.Apk;
import com.keeley.spider.server.AbsSpiderFacade;
import com.keeley.spider.util.ApkUtil;
import com.keeley.spider.util.PropertiesUtil;
import org.apache.log4j.Logger;

import java.util.List;


public class Client {
    public static Logger logger = Logger.getLogger(Client.class);
    public static void main(String []args){
        long startTime = System.currentTimeMillis();
        initDateBase();
        List<String > apks= ApkUtil.getText("apk.txt");

        AbsSpiderFacade baiduFacade = AbsSpiderFacade.createBaiduFacade();
        AbsSpiderFacade z360Facade =  AbsSpiderFacade.create360Facade();
        baiduFacade.setNextFacade(z360Facade); //责任链

        for(String apk :apks){
            try{
                if(baiduFacade.checkUpdate(apk))
                    logger.info("结果========"+apk+"需要更新");
                else
                    logger.info("结果========"+apk+"不需要更新");
                logger.info(" ");
            }catch (Exception e){
                logger.error(apk+"更新出错,原因:"+e.getMessage());
            }
        }

        logger.info("执行完毕 耗时 "+((System.currentTimeMillis()-startTime)/1000)+"秒");
    }



    private static void initDateBase() {
        C3p0Plugin c3p0Plugin = new C3p0Plugin(PropertiesUtil.get("jdbcUrl"),PropertiesUtil.get("user"),
                PropertiesUtil.get("password").trim(),PropertiesUtil.get("driver"));
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        arp.setDialect(new Sqlite3Dialect()) ;
        arp.setDevMode(false);
        arp.addMapping("p_apk", Apk.class);

        c3p0Plugin.start();
        arp.start();
    }
}

